package com.example.myapplication;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.myapplication.databinding.CaculatorActivityMainBinding;


public class CaculatorActivity extends AppCompatActivity implements View.OnClickListener {
    String TAG = "MainActivity";

    // Used to load the 'caculator' library on application startup.
    static {
        System.loadLibrary("caculator");
    }

    private CaculatorActivityMainBinding binding;
    private EditText et_parameter1;
    private EditText et_parameter2;
    private Button button_c_add;
    private TextView tv_result;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
    }

    private void initView() {
        binding = CaculatorActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        TextView tv = binding.sampleText;
        tv.setText(stringFromJNI());
        et_parameter1 = binding.etParameter1;
        et_parameter2 = binding.etParameter2;
        button_c_add = binding.buttonAdd;
        tv_result = binding.tvResult;
    }

    private void initData() {
        button_c_add.setOnClickListener(this);
        binding.buttonMinus.setOnClickListener(this);
        binding.buttonDivide.setOnClickListener(this);
        binding.buttonMultiply.setOnClickListener(this);
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        Log.i(TAG, "onClick view " + view + ",begin");
        String result = "";
        String parameter1 = "";
        String parameter2 = "";
        char type = '&';
        //此处使用switch报错
        if (view.getId() == R.id.button_add) {
            type = '+';
        } else if (view.getId() == R.id.button_minus) {
            type = '-';
        } else if (view.getId() == R.id.button_multiply) {
            type = '*';
        } else if (view.getId() == R.id.button_divide) {
            type = '/';
        }
        Toast.makeText(CaculatorActivity.this, "按钮被点击了", Toast.LENGTH_SHORT).show();

        Log.i(TAG, "onClick view = " + view + ",end type = " + type);
        parameter1 += getParameter1FloatType();
        parameter2 += getParameter2FloatType();
        result = "" + operationNumberFromJNI(getParameter1FloatType(), getParameter2FloatType(), type);
        tv_result.append("\n" + parameter1 + " " + type + " " + parameter2 + " = " + result);
    }

    private native float operationNumberFromJNI(float parameter1FloatType, float parameter2FloatType, char type);
    private float getParameter1FloatType() {
        try {
            return Float.parseFloat("" + et_parameter1.getText().toString());
        } catch (NumberFormatException e) {
            Log.e(TAG, "error = ", e);
            return Integer.MAX_VALUE;
        }
    }
    private float getParameter2FloatType() {
        try {
            return Float.parseFloat("" + et_parameter2.getText().toString());
        } catch (NumberFormatException e) {
            Log.e(TAG, "error = ", e);
            return Integer.MAX_VALUE;
        }
    }
    /**
     * A native method that is implemented by the 'caculator' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();
}